package com.tjhk.java.tech.thread.pool;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.concurrent.*;
import java.util.stream.IntStream;

/**
 * @author ：little grape
 * @description：TODO
 * @date ：2023/4/18 22:15
 */
public class ExecutorPoolTest {
    public static void main(String[] args) {
        LocalDateTime st = LocalDateTime.now();
        System.out.println(Runtime.getRuntime().availableProcessors());
        ExecutorService exc = Executors.newWorkStealingPool();
        IntStream.range(0,1000_000)
                .mapToObj(
                        i->new Thread("线程"+i){
                            @Override
                            public void run() {
                                System.out.println("线程"+i+"正在执行！");
                            }
                        }).forEach(exc::submit);
        ExecutorService exc1 = Executors.newSingleThreadExecutor();
        IntStream.range(0,10)
                .mapToObj(i->new Callable<String>() {
                    @Override
                    public String call() throws Exception {
                        System.out.println("线程"+i+"正在执行！");
                        return "线程"+i;
                    }
                }).forEach(t->{
                    try {
                        System.out.println("返回："+exc1.submit(t).get());
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    } catch (ExecutionException e) {
                        throw new RuntimeException(e);
                    }
                });
        ExecutorService exc2 = Executors.newCachedThreadPool();
        IntStream.range(0,1000_000)
                .mapToObj(i->new Thread("线程"+i){
                    @Override
                    public void run() {
                        System.out.println(this.getName()+"正在执行！");
                    }
                }).forEach(exc2::submit);
        System.out.println("consume time "+(LocalDateTime.now().getNano() - st.getNano()));
    }
}
